package mathlib;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FormatChanger {

	public static void main(String[] args) throws IOException {
		
		String[] headers = null;
		String[] units = null;
		List<String[]> data = new ArrayList<>();
		
		File inputFile = new File("D:\\soar export.txt");
		
		Scanner input = new Scanner(inputFile);
		
		int index = 0;
		while(input.hasNext()) {
			String line = input.nextLine().trim();
			if (line.equals("")) {
				continue;
			}
			else if (index == 3) {
				headers = line.split("\t");
				for (int i = 0; i < headers.length; i++) {
					headers[i] = headers[i].trim();
				}
			}
			else if (index == 5) {
				units = line.split("\t");
				for (int i = 0; i < units.length; i++) {
					String oddChar = (char)194 + "";
					units[i] = units[i].trim().replace(oddChar, "");
				}
			}
			else if (index >= 7){
				String[] dataLine = line.split("\t");
				for (int i = 0; i < dataLine.length; i++) {
					dataLine[i] = dataLine[i].trim();
				}
				data.add(dataLine);
			}
			index++;
		}
		
		File outputFile = new File("D:\\Soar Data.csv");
		FileWriter fw = new FileWriter(outputFile);
		PrintWriter pw = new PrintWriter(fw);
		
		for(int i = 0; i < headers.length; i++) {
			pw.print(headers[i]);
			if (i < headers.length - 1) {
				pw.print(",");
			}
			else {
				pw.println("");
			}
		}
		for(int i = 0; i < units.length; i++) {
			pw.print(units[i]);
			if (i < units.length - 1) {
				pw.print(",");
			}
			else {
				pw.println("");
			}
		}
		for(String[] line : data) {
			for (int i = 0; i < line.length; i++) {
				pw.print(line[i]);
				if (i < units.length - 1) {
					pw.print(",");
				}
				else {
					pw.println("");
				}
			}
		}
		
		pw.close();
		fw.close();
		
	}

}
